package com.cq.controller;

import com.cq.pojo.Employee;
import com.cq.pojo.LoginUser;
import com.cq.pojo.Student;
import com.cq.pojo.User;
import com.cq.service.EmployeeService;
import com.cq.service.StuService;
import com.cq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/student/normal")
public class QueryController {

    @Autowired
    private UserService userService;

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private StuService stuService;

    @RequestMapping("/basic")
    public String stuQueryByLogin(HttpServletRequest req,ModelMap modelMap){
        LoginUser key = (LoginUser) req.getSession().getAttribute("loginUser");
        char c = key.getUsername().charAt(0);
        if (c == 'A' ){
            String user_id = key.getUsername();
            User user = userService.queryUser(user_id);
            modelMap.put("code",1);
            modelMap.put("user",user);
        }else if (c == 'E'){
            String emp_id = key.getUsername();
            Employee emp = employeeService.queryOne(emp_id);
            modelMap.put("code","2");
            modelMap.put("emp",emp);
        }
        else {
            int id = Integer.parseInt(key.getUsername());
            Student stu = stuService.queryOneByLogin(id);
            modelMap.put("code","3");
            modelMap.put("stu",stu);
        }
        return "query";
    }

    @RequestMapping("/queryAllUser")
    public String queryAllUser(ModelMap modelMap) {
        List<User> users = userService.queryAllUser();
        modelMap.put("users",users);
        return "allUsers";
    }

    @RequestMapping("/queryAllStu")
    public String queryAllStu(ModelMap modelMap){
        List<Student> stus = stuService.queryAllStus();
        modelMap.put("stus",stus);
        return "allStus";
    }

    @RequestMapping("/queryAllEmp")
    public String queryAllEmp(ModelMap modelMap) {
        List<Employee> emps = userService.queryAllEmp();
        modelMap.put("emps", emps);
        return "allEmps";
    }
}
